package com.ailk.tcm.hffw.android.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.bitmap.display.Displayer;
import net.tsz.afinal.bitmap.display.SimpleDisplayer;
import android.app.Activity;
import android.app.Application;
import android.os.Environment;

import com.ailk.tcm.R;
import com.ailk.tcm.hffw.android.common.network.HttpUtil;

public class MyApplication extends Application{
	
	/**
	 * 本设备的像素密度
	 */
	public static float density = -1;
	public static String DOWNLOAD_STORE_PATH = Environment.getExternalStorageDirectory() + "/tcm_download/";
	public static String DOWNLOAD_IMAGES_STORE_PATH = DOWNLOAD_STORE_PATH + "/images/";
	static {
		File targetDir = new File(MyApplication.DOWNLOAD_IMAGES_STORE_PATH);
		targetDir.mkdirs();
	}
	
	public static FinalDb finalDb;
	public static final HttpUtil httpUtil = new HttpUtil();
	//全局图片缓存/加载器
	public static FinalBitmap imageLoader;
	public static Displayer imageDisplayer = new SimpleDisplayer();
	
	private static MyApplication instance = null;
	private List<Activity> aliveActivities = new ArrayList<Activity>();
	private Activity currentActivity;
	
	//如何访问服务器
//	public static String SERVER = "192.168.1.103";
	public static String SERVER = "182.92.79.206";
	public static final int PORT = 8080;
	public static final String PROTOCOL = "http";
	public static final String PREFIX = "weizu/api/v1";
	public static String BASE_URL = PROTOCOL + "://" + SERVER + ":" + PORT + "/" + PREFIX;
	public static String UPDATE_URL = PROTOCOL + "://" + SERVER + ":" + PORT + "/weizu_update";
	
    public static final String JPUSHER_HOST = SERVER;
	public static final int JPUSHER_PORT = 11987;
    
	public static MyApplication getInstance(){
		return instance;
	}
	
	@Override
    public void onCreate() {
		init();
	    super.onCreate();
	}
	
	private void init(){
		instance = this;
		density = getResources().getDisplayMetrics().density;
		finalDb = FinalDb.create(this);
		imageLoader = FinalBitmap.create(this);
		imageLoader.configBitmapLoadThreadSize(3);
		imageLoader.configDiskCachePath(getFilesDir().toString());
		imageLoader.configDiskCacheSize(1024 * 1024 * 20);//设置缓存大小 
		imageLoader.configLoadingAnimationDrawble(R.drawable.loading);
		imageLoader.configDisplayer(imageDisplayer);
	}
	
	public void onActivityCreate(Activity activity){
		addActivity(activity);
	}
	
	public void onActivityDestroy(Activity activity){
		removeActivity(activity);
	}
	
	public void onActivityResume(Activity activity){
		setCurrentActivity(activity);
	}
	
	public void onActivityStop(Activity activity){
		setCurrentActivity(null);
	}
	
	private void addActivity(Activity activity){
		aliveActivities.add(activity);
	}
	
	private void removeActivity(Activity activity){
		aliveActivities.remove(activity);
	}
	
	private void setCurrentActivity(Activity activity){
		currentActivity = activity;
	}
	
	/**
	 * 获取所有仍然存活的activity
	 * @return
	 */
	public Iterator<Activity> getAliveActivities() {
		return aliveActivities.iterator();
	}

	/**
	 * 获取当前activity
	 * @return	如果当前activity不在该应用，则返回null
	 */
	public Activity getCurrentActivity() {
		return currentActivity;
	}
	
	/**
	 * 结束所有该应用的activity
	 */
	public void finishAllActivity(){
		for(Activity a : aliveActivities){
			try {
				a.finish();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 退出应用
	 */
	public void exit(){
		finishAllActivity();
		//管理service
		//管理receiver
		//管理其他线程
		try {
			MyAudioManager.recycle();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
